package com.dalonedrow.engine.ui.impl.border;

import com.dalonedrow.engine.sprite.base.SpriteBase;
import com.dalonedrow.engine.sprite.impl.BasicTriangleSpriteObject;
import com.dalonedrow.engine.systems.JOGLErrorHandler;
import com.dalonedrow.engine.systems.Render;
import com.dalonedrow.engine.systems.Sprite;
import com.dalonedrow.engine.ui.base.border.GuiBorder;
import com.dalonedrow.globalvariables.constants.hq.GlobalConstants;

/**
 * A simple border, similar to those found in the early 90's SNES games.
 * @author Donald
 */
public final class H1SelectionBorder extends GuiBorder {
	/** the height of the selection border. */
	private int height = 0;
	/** Creates a new instance of {@link H1SelectionBorder}. */
	public H1SelectionBorder() {
		final int eighteen = 18;
		super.setInsets(
				0,
				eighteen,
				0,
				eighteen
				);

		SpriteBase sb = new BasicTriangleSpriteObject();
		sb.setX1(0);
		sb.setY1(0);
		sb.setX2(0);
		sb.setY2(15f);
		sb.setX3(15f);
		sb.setY3(7.5f);
		super.setSprite(sb);
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void prepareForRendering() throws Exception {
		Render.getInstance().registerEntity(super.getSprite());
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void setPosition(final double x, final double y, final double z) {
		super.setPosition(x, y, z);
		try {
			Sprite.getInstance().computeFullStats(super.getSprite());
		} catch (Exception ex) {
			JOGLErrorHandler.getInstance().fatalError(ex);
		}
		double sy =
			y + ((height - super.getSprite().getHeight()) 
					* GlobalConstants.DIV_2);
		super.getSprite().setPosition(
				x, 
				sy, 
				z
				);
		super.getSprite().clearFloatStore();
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void setPreferredSize(final int w, final int h) {
		super.setPreferredSize(w, h);
		// position the 
		this.height = h;
		super.getSprite().clearFloatStore();
	}
}
